<template>
  <div style="height:100%;position:relative;">
    <div :id="id" style="height: 100%" :style="{visibility:data&&data.length>0?'visible':'hidden'}"></div>
    <div v-show="!data||data.length===0" class="no-data">暂无数据</div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import 'echarts-liquidfill'

export default {
  name: "liquid",
  props: {
    id: {
      type: String,
      default: "liquid",
    },
    color: {
      type: Array,
      default: () => [],
    },
    data: {
      type: Array,
      default: () => [],
    },
  },
  watch: {
    data: {
      deep: true,
      handler(nVal) {
        if (this.chart) {
          this.setOption();
        } else {
          this.initChart();
        }
      },
    },
  },
  data() {
    return {
      chart: null,
			xData: [],
			yData: []
    };
  },
  mounted() {
		this.xData = this.data.map((item) => {
			return item.name;
		});
		if (this.xData.length === this.color.length && this.xData.length != 0) {
			this.yData = this.data.map((item) => {
				return item.count;
			});
			this.initChart();
		} else {
			console.log('颜色数组和数据数组需要一一对应')
		}
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id), "macarons");
      this.setOption();

      this.chart.on("click", (param) => {
        this.$emit("onClick", this.data[param.dataIndex].key);
      });

      window.addEventListener("resize", () => {
        this.chart && this.chart.resize();
      });
    },
    setOption() {
      if (!this.data || Object.keys(this.data).length === 0) {
        return;
      }

      let option = {
				grid: {
          left: 0,
          right: 0,
          bottom: 0,
          top: 0,
          containLabel: true,
        },
				legend: {
					orient: "vertical",
          right: 0,
          top: "middle",
          icon: "circle",
          itemGap: 20,
          itemWidth: 12,
          itemHeight: 12,
          selectedMode: false,
          textStyle: {
            color: "#82A9EF",
          },
          data: this.xData,
				},
				tooltip: {
					show: true
				},
        series: [
          {
            center: ["45%", "50%"],
						radius: '75%',
						type: "liquidFill",
            data: this.yData,
						color: this.color,
						label: {
							fontSize: 24,
              position: ['50%', '36%'],
						},
						outline : { 
            	borderDistance: 0, 
            	itemStyle: { 
								borderWidth: 2, 
								borderColor: '#156ACF',
								shadowBlur: 20, 
								shadowColor: 'rgba(255, 255, 255, 1)' 
							}
						} 
          },
          {
            type: 'pie',
            data: this.data
          }
        ],
      };

      this.chart.setOption(option);
    },
  },
};
</script>

<style lang="less" scoped>
.no-data {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #c5c8ce;
}
</style>